<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

use Grafika\Grafika;
class ImageResize {

    private $CI;
    private $Grafika;
    private $avatar_path;
    public function __construct()
    {
        $this->CI =& get_instance();
        $this->avatar_path = FCPATH.'uploads/avatar/';
        $this->uid = $this->CI->session->userdata('user_id');
        if($this->uid){
            $this->path = $this->avatar_path.$this->get_dir($this->uid);
            $this->avatar_path_url = base_url('uploads/avatar/'.$this->get_dir($this->uid));
            if(!file_exists($this->path)){
                mkdir($this->path,0777,true);
            }
        }
        $this->Grafika = Grafika::createEditor();
    }

    public function resize_avatar($avatar){
        $avatar_thumbnail = config_item("avatar_thumbnail");
        foreach ($avatar_thumbnail as $key => $value){
            $this->Grafika->open($thumb , $avatar);
            $this->Grafika->resizeFill($thumb , $value['w'] , $value['h']);
            $this->Grafika->save($thumb , $this->avatar_path.$this->get_dir($this->uid).$this->uid.'_avatar_'.$key.'.png');
        }
        return true;
    }

    public function get_dir($uid) {
        $uid = sprintf ( "%02d", $uid );
        $dir1 = substr ( $uid, -1, 1 );
        $dir2 = substr ( $uid, -2, 2 );
        return $dir1 . '/' . $dir2. '/';
    }

    public function resize_goods($upload_path,$file_name){
        $goods_thumbnail = config_item("goods_thumbnail");
        foreach ($goods_thumbnail as $key => $value){
            $this->Grafika->open($thumb , $upload_path.$file_name);
            $this->Grafika->resizeFit($thumb , $value['w'] , $value['h']);
            $this->Grafika->save($thumb , $upload_path.$key."_".$file_name);
        }
        return true;
    }

    public function resize_goods_detail($upload_path,$file_name){
        $goods_detail_thumbnail = config_item("goods_detail_thumbnail");
        foreach ($goods_detail_thumbnail as $key => $value){
            $this->Grafika->open($thumb , $upload_path.$file_name);
            $this->Grafika->resizeExactWidth($thumb , $value['w']);
            $this->Grafika->save($thumb , $upload_path.$key."_".$file_name);
        }
        return true;
    }


}

/* End of file AvatarResize.php */
/* Location: ./application/libraries/AvatarResize.php */
